package nuc.wy;

import nuc.SQLoperation.Constant;
import nuc.SQLoperation.SQLiteOperationHelper;
import nuc.widget.Parse;
import nuc.widget.WidgetActivity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class SimpleNoteActivity extends ListActivity {
	/** Called when the activity is first created. */
	private TextView textview = null;
	ListView listview;
	SimpleAdapter adapter;
	SimpleCursorAdapter cursoradapter;
	Cursor cursor ;
	SQLiteOperationHelper DBHelper;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(Menu.NONE, 0, 0, R.string.reset).setIcon(android.R.drawable.ic_menu_revert);
		menu.add(Menu.NONE,1,0,R.string.about).setIcon(android.R.drawable.ic_menu_info_details);
		menu.add(Menu.NONE, 2, 0,R.string.exit).setIcon(android.R.drawable.ic_menu_rotate);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
        case 0:
//CASE 0 ========================>Start
        	AlertDialog.Builder builder = new Builder(SimpleNoteActivity.this);
			builder.setTitle(R.string.alert);
			builder.setMessage(R.string.reset);
			builder.setPositiveButton(R.string.reset_all, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					DBHelper = new SQLiteOperationHelper(SimpleNoteActivity.this, Constant.DB_NAME, null, 1);
					if(DBHelper.reset()){
						Toast.makeText(SimpleNoteActivity.this, R.string.reset_succeed, Toast.LENGTH_SHORT).show();
						SimpleNoteActivity.this.onResume();
						WidgetActivity widgetUpdate = new WidgetActivity();
						widgetUpdate.update(SimpleNoteActivity.this);
					}
				}
			});
			builder.setNegativeButton(R.string.cancel_delete, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			builder.show();
            break;
//CASE 0 <========================End
        case 1:
        	Intent intent = new Intent(SimpleNoteActivity.this, About.class);
        	startActivity(intent);
            break;
        case 2:
            finish();
            break;
     }
		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);

		DBHelper = new SQLiteOperationHelper(
				SimpleNoteActivity.this, Constant.DB_NAME, null, 1);
		cursor = DBHelper.query("_id desc");

		listview = getListView();
		cursoradapter = new SimpleCursorAdapter(SimpleNoteActivity.this,
				R.layout.notelist, cursor,
				new String[] { "_title", "_content" }, new int[] { R.id.Data,
						R.id.TitleAndContent });
		listview.setAdapter(cursoradapter);
		listview.setCacheColorHint(0);
		listview.setOnItemClickListener(new ListviewListener());

		textview = (TextView) findViewById(R.id.MainclickableButton);
		textview.setOnClickListener(new AddItemListener());
		textview.setClickable(true);
		DBHelper.close();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		DBHelper = new SQLiteOperationHelper(
				SimpleNoteActivity.this, Constant.DB_NAME, null, 1);
		cursor = DBHelper.query("_id desc");

		listview = getListView();
		cursoradapter = new SimpleCursorAdapter(SimpleNoteActivity.this,
				R.layout.notelist, cursor,
				new String[] { "_title", "_content" }, new int[] { R.id.Data,
						R.id.TitleAndContent });
		listview.setAdapter(cursoradapter);
		DBHelper.close();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
//		cursor.close();
	}

	class AddItemListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

			Intent intent = new Intent(SimpleNoteActivity.this,
					CreateNote.class);
			SimpleNoteActivity.this.startActivity(intent);
			overridePendingTransition(android.R.anim.fade_in,
					android.R.anim.fade_out);

		}
	}

	class ListviewListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(SimpleNoteActivity.this,
					NoteContent.class);
			DBHelper = new SQLiteOperationHelper(
					SimpleNoteActivity.this, Constant.DB_NAME, null, 1);
			cursor = DBHelper.queryByid(String.valueOf(arg0.getItemIdAtPosition(arg2)));
			if (cursor != null) {
				SimpleNote note;
				cursor.moveToFirst();
				note = Parse.parseCursor(cursor);

				intent.putExtra("note", note);
				int position = arg0.getPositionForView(arg1);
				intent.putExtra("position", position);
				System.out.println(position);
				startActivity(intent);
				DBHelper.close();
			}
			DBHelper.close();

		}

		class DelListener implements OnItemClickListener {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				DBHelper = new SQLiteOperationHelper(
						SimpleNoteActivity.this, Constant.DB_NAME, null, 1);
				DBHelper.del(arg0.getItemIdAtPosition(arg2));
				DBHelper.close();

			}

		}		
	}
}